﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YJKRevitCurveDrag;
using YJKExcelFormTool.DataDefine;
using YJKExcelFormTool.Utilities;

namespace YJKExcelFormTool.Kits
{
	public class TableDrawingToRevitKit
	{
		public TableDrawingToRevitKit(ExternalCommandData cmdData, TableInfos tableInfos, Autodesk.Revit.DB.View view = null)
		{
			this.m_Revit = cmdData;
			this.m_ptLeftTop = XYZ.Zero;
			if (tableInfos.AutoCellsSize)
			{
				this.m_CellAutoSize = TableDrawingToRevitKit._TagTextComosingMode.TCM_HORIZONTAL_ZOOM;
			}
			else
			{
				this.m_CellAutoSize = TableDrawingToRevitKit._TagTextComosingMode.TCM_VERTICAL_ZOOM;
			}
			this.m_DrawingView = view;
			if (this.m_DrawingView == null)
			{
				this.m_DrawingView = this.m_Revit.Application.ActiveUIDocument.ActiveView;
			}
			this.m_DirX = this.m_DrawingView.RightDirection;
			this.m_DirY = this.m_DrawingView.UpDirection;
			this.m_DirZ = this.m_DrawingView.ViewDirection;
			this.m_DrawingScale = (double)this.m_DrawingView.Scale;
			this.m_TableInfos = tableInfos;
			this.SetTextTypeInfo(tableInfos.FontSetting);
		}

		public Result Drawing(bool drag = true)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				this.CalculateTableCellsSize();
				this.GetTableHeaderDrawingInfoByInfos(this.m_TableInfos.Head.TableHeadInfos);
				this.GetTableDrawingInfoBody();
				this.GetRightBottomLineDrawingInfo();
				if (drag && !DragCurveJigInterface.GetInsertPosition(this.m_Revit, this.m_DragCurveInfos, false, this.m_ptLeftTop, ref this.m_ptLeftTop))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					transaction = new Transaction(this.m_Revit.Application.ActiveUIDocument.Document);
					transaction.Start("Drawing table");
					this.SetRasterImagesVisibility();
					this.DrawTableHeaderByInfos(this.m_TableInfos.Head.TableHeadInfos);
					this.DrawTableBody();
					this.DrawRightBottomLine();
					Group group = this.CreateGroup();
					transaction.Commit();
					if (group != null)
					{
						this.ZoomElement(group);
					}
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (transaction != null)
				{
					transaction.RollBack();
				}
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private void SetRasterImagesVisibility()
		{
			foreach (object obj in this.m_Revit.Application.ActiveUIDocument.Document.Settings.Categories)
			{
				Category category = (Category)obj;
				if (category.Id.IntegerValue == -2000560)
				{
					try
					{
						this.m_DrawingView.SetYJKVisibility(category, true);
					}
					catch (Exception)
					{
					}
				}
			}
		}

		private void SetTextTypeInfo(TextNoteType textType)
		{
			if (textType == null)
			{
				return;
			}
			this.m_TextNoteType = textType;
			Parameter parameter = textType.get_Parameter(BuiltInParameter.TEXT_SIZE);
			if (parameter == null)
			{
				throw new Exception("parameter 'TEXT_SIZE' is null.");
			}
			this.m_TextSize = parameter.AsDouble();
			Parameter parameter2 = textType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE);
			if (parameter2 == null)
			{
				throw new Exception("parameter 'TEXT_WIDTH_SCALE' is null.");
			}
			this.m_TextWidthScale = parameter2.AsDouble();
		}

		private void CalculateTableCellsSize()
		{
			if (this.m_CellAutoSize == TableDrawingToRevitKit._TagTextComosingMode.TCM_HORIZONTAL_ZOOM)
			{
				this.CalculateTableColumnsWidthByHZoom();
				this.CalculateTableRowsHeightByHZoom();
				return;
			}
			if (this.m_CellAutoSize == TableDrawingToRevitKit._TagTextComosingMode.TCM_VERTICAL_ZOOM)
			{
				this.CalculateTableColumnsWidthByVZoom();
				this.CalculateTableRowsHeightByVZoom();
				return;
			}
			this.CalculateTableColumnsWidthByVZoom();
			this.CalculateTableRowsHeightByHZoom();
		}

		private void CalculateTableColumnsWidthByHZoom()
		{
			List<TableHeadCell> effectiveRow = this.m_TableInfos.Head.GetEffectiveRow();
			for (int i = 0; i < effectiveRow.Count; i++)
			{
				TableHeadCell tableHeadCell = effectiveRow[i];
				double num = this.m_TableInfos.Head.CellWidth(effectiveRow[i]);
				this.m_ColumnWidthList.Add(num);
				int num2 = this.GetLength(tableHeadCell.Key.KeyName);
				if (num2 == 0)
				{
					if (tableHeadCell.InfoType == TableHeadCell.CellInfoType.CIT_Text)
					{
						num2 = this.GetLength(tableHeadCell.Value);
					}
					else
					{
						num2 = 8;
					}
				}
				double num3 = ((double)num2 + this.m_TableInfos.HMargin) * this.m_TextSize * this.m_TextWidthScale * 1.2;
				if (num3 > num)
				{
					this.m_ColumnWidthList[i] = num3;
				}
			}
			foreach (TableBodyRow tableBodyRow in this.m_TableInfos.Body.BodyData)
			{
				for (int i = 0; i < tableBodyRow.RowData.Count; i++)
				{
					double num4 = 0.0;
					TableBodyCell tableBodyCell = tableBodyRow.RowData[i];
					if (tableBodyCell.DataType == TableBodyCell._tagDataType._DT_TEXT)
					{
						num4 = ((double)this.GetLength(tableBodyCell.DataValue) + this.m_TableInfos.HMargin) * this.m_TextSize * this.m_TextWidthScale * 1.2;
					}
					else
					{
						TableBodyCell._tagDataType dataType = tableBodyCell.DataType;
					}
					if (num4 > this.m_ColumnWidthList[i])
					{
						this.m_ColumnWidthList[i] = num4;
					}
				}
			}
			for (int j = 0; j < this.m_ColumnWidthList.Count; j++)
			{
				this.m_TableInfos.Head.SetEffectiveRowItemWidthByIndex(j, this.m_ColumnWidthList[j]);
			}
		}

		private void CalculateTableRowsHeightByHZoom()
		{
			foreach (double item in this.m_TableInfos.Head.GetCellHeightList())
			{
				this.m_RowHeightList.Add(item);
			}
			double num = 0.026246719160104987;
			foreach (TableBodyRow tableBodyRow in this.m_TableInfos.Body.BodyData)
			{
				double num2 = num;
				for (int i = 0; i < tableBodyRow.RowData.Count; i++)
				{
					double num3 = 0.0;
					TableBodyCell tableBodyCell = tableBodyRow.RowData[i];
					if (tableBodyCell.DataType != TableBodyCell._tagDataType._DT_IMAGE && tableBodyCell.DataType != TableBodyCell._tagDataType._DT_SYMBOL)
					{
						num3 = num;
					}
					if (num3 > num2)
					{
						num2 = num3;
					}
				}
				this.m_RowHeightList.Add(num2);
			}
		}

		private void CalculateTableColumnsWidthByVZoom()
		{
			List<TableHeadCell> effectiveRow = this.m_TableInfos.Head.GetEffectiveRow();
			for (int i = 0; i < effectiveRow.Count; i++)
			{
				this.m_ColumnWidthList.Add(this.m_TableInfos.Head.CellWidth(effectiveRow[i]));
			}
		}

		private void CalculateTableRowsHeightByVZoom()
		{
			foreach (double item in this.m_TableInfos.Head.GetCellHeightList())
			{
				this.m_RowHeightList.Add(item);
			}
			foreach (TableBodyRow tableBodyRow in this.m_TableInfos.Body.BodyData)
			{
				double num = 0.026246719160104987;
				for (int i = 0; i < tableBodyRow.RowData.Count; i++)
				{
					double num2 = this.m_TableInfos.Head.GetCellWidthList()[i];
					double num3 = 0.0;
					TableBodyCell tableBodyCell = tableBodyRow.RowData[i];
					if (tableBodyCell.DataType != TableBodyCell._tagDataType._DT_IMAGE && tableBodyCell.DataType != TableBodyCell._tagDataType._DT_SYMBOL)
					{
						double length = (double)this.GetLength(tableBodyCell.DataValue);
						double num4 = this.m_TextSize * this.m_TextWidthScale;
						int num5 = (int)Math.Ceiling(length * num4 / (num2 - this.m_TableInfos.HMargin * num4));
						num3 = this.m_TextSize * 1.2 * (double)num5;
					}
					if (num3 > num)
					{
						num = num3;
					}
				}
				this.m_RowHeightList.Add(num);
			}
		}

		private void GetTableHeaderDrawingInfoByInfos(List<TableHeadCell> headerData)
		{
			this.CalculateTableWidth();
			this.CalculateHeaderHeight();
			foreach (TableHeadCell tableHeadCell in headerData)
			{
				double num = 0.0;
				double num2 = 0.0;
				this.m_TableInfos.Head.CellPosition(tableHeadCell, ref num, ref num2);
				double width = this.m_TableInfos.Head.CellWidth(tableHeadCell) * this.m_DrawingScale;
				double height = this.m_TableInfos.Head.CellHeight(tableHeadCell) * this.m_DrawingScale;
				XYZ ptLeftTop = this.m_ptLeftTop + num * this.m_DrawingScale * this.m_DirX;
				ptLeftTop = this.m_ptLeftTop - num2 * this.m_DrawingScale * this.m_DirY;
				TableBodyCell cellData = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, tableHeadCell.Value);
				this.GetCellDrawingInfo(ptLeftTop, width, height, cellData);
			}
		}

		private void DrawTableHeaderByInfos(List<TableHeadCell> headerData)
		{
			foreach (TableHeadCell tableHeadCell in headerData)
			{
				double num = 0.0;
				double num2 = 0.0;
				this.m_TableInfos.Head.CellPosition(tableHeadCell, ref num, ref num2);
				double width = this.m_TableInfos.Head.CellWidth(tableHeadCell) * this.m_DrawingScale;
				double height = this.m_TableInfos.Head.CellHeight(tableHeadCell) * this.m_DrawingScale;
				XYZ xyz = this.m_ptLeftTop + num * this.m_DrawingScale * this.m_DirX;
				xyz -= num2 * this.m_DrawingScale * this.m_DirY;
				TableBodyCell cellData;
				if (tableHeadCell.InfoType == TableHeadCell.CellInfoType.CIT_Text)
				{
					cellData = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, tableHeadCell.Value);
				}
				else if (tableHeadCell.InfoType == TableHeadCell.CellInfoType.CIT_Image)
				{
					cellData = new TableBodyCell(TableBodyCell._tagDataType._DT_IMAGE, tableHeadCell.Value);
				}
				else
				{
					if (tableHeadCell.InfoType != TableHeadCell.CellInfoType.CIT_ConstImage)
					{
						continue;
					}
					cellData = new TableBodyCell(TableBodyCell._tagDataType._DT_ConstImage, tableHeadCell.Value);
				}
				this.DrawCell(xyz, width, height, cellData);
			}
		}

		private void GetCellDrawingInfo(XYZ ptLeftTop, double width, double height, TableBodyCell cellData)
		{
			XYZ direction = this.m_DirX * width;
			XYZ direction2 = -this.m_DirY * height;
			this.GetModelLine(ptLeftTop, direction);
			this.GetModelLine(ptLeftTop, direction2);
		}

		private void DrawCell(XYZ ptLeftTop, double width, double height, TableBodyCell cellData)
		{
			XYZ direction = this.m_DirX * width;
			XYZ direction2 = -this.m_DirY * height;
			this.DrawModelLine(ptLeftTop, direction);
			this.DrawModelLine(ptLeftTop, direction2);
			if (cellData.DataType == TableBodyCell._tagDataType._DT_TEXT)
			{
				this.DrawText(ptLeftTop, width, height, cellData.DataValue);
				return;
			}
			if (cellData.DataType == TableBodyCell._tagDataType._DT_SYMBOL)
			{
				this.DrawLegendComponent(ptLeftTop, width, height, cellData.DataValue, true);
				return;
			}
			if (cellData.DataType == TableBodyCell._tagDataType._DT_IMAGE)
			{
				this.DrawLegendComponent(ptLeftTop, width, height, cellData.DataValue, true);
				return;
			}
			if (cellData.DataType == TableBodyCell._tagDataType._DT_ConstImage)
			{
				this.DrawLegendComponent(ptLeftTop, width, height, cellData.DataValue, true);
			}
		}

		private void GetTableDrawingInfoBody()
		{
			this.CalculateBodyHeight();
			double x = this.m_ptLeftTop.X;
			double num = this.m_ptLeftTop.Y - this.m_HeaderHeight;
			int num2 = this.m_TableInfos.Head.RowSize;
			foreach (TableBodyRow tableBodyRow in this.m_TableInfos.Body.BodyData)
			{
				double num3 = x;
				double num4 = this.CellHeight(new List<int>
				{
					num2
				});
				for (int i = 0; i < tableBodyRow.RowData.Count; i++)
				{
					double num5 = this.CellWidth(new List<int>
					{
						i
					});
					XYZ ptLeftTop = new XYZ(num3, num, 0.0);
					this.GetCellDrawingInfo(ptLeftTop, num5, num4, tableBodyRow.RowData[i]);
					num3 += num5;
				}
				num2++;
				num -= num4;
			}
		}

		private void DrawTableBody()
		{
			XYZ xyz = this.m_ptLeftTop - this.m_DirY * this.m_HeaderHeight;
			int num = this.m_TableInfos.Head.RowSize;
			foreach (TableBodyRow tableBodyRow in this.m_TableInfos.Body.BodyData)
			{
				double num2 = 0.0;
				double num3 = this.CellHeight(new List<int>
				{
					num
				});
				for (int i = 0; i < tableBodyRow.RowData.Count; i++)
				{
					double num4 = this.CellWidth(new List<int>
					{
						i
					});
					XYZ ptLeftTop = xyz + num2 * this.m_DirX;
					this.DrawCell(ptLeftTop, num4, num3, tableBodyRow.RowData[i]);
					num2 += num4;
				}
				num++;
				xyz -= this.m_DirY * num3;
			}
		}

		private void GetRightBottomLineDrawingInfo()
		{
			double tableWidth = this.m_TableWidth;
			double num = this.m_HeaderHeight + this.m_BodyHeight;
			double x = this.m_ptLeftTop.X;
			double y = this.m_ptLeftTop.Y;
			XYZ ptStart = new XYZ(x, y - num, 0.0);
			XYZ direction = XYZ.BasisX * tableWidth;
			XYZ direction2 = -XYZ.BasisY * num;
			this.GetModelLine(ptStart, direction);
			XYZ ptStart2 = new XYZ(x + tableWidth, y, 0.0);
			this.GetModelLine(ptStart2, direction2);
		}

		private void DrawRightBottomLine()
		{
			double tableWidth = this.m_TableWidth;
			double num = this.m_HeaderHeight + this.m_BodyHeight;
			double x = this.m_ptLeftTop.X;
			double y = this.m_ptLeftTop.Y;
			XYZ ptStart = this.m_ptLeftTop - this.m_DirY * num;
			XYZ direction = this.m_DirX * tableWidth;
			XYZ direction2 = -this.m_DirY * num;
			this.DrawModelLine(ptStart, direction);
			XYZ ptStart2 = this.m_ptLeftTop + this.m_DirX * tableWidth;
			this.DrawModelLine(ptStart2, direction2);
		}

		private void GetModelLine(XYZ ptStart, XYZ direction)
		{
			XYZ endPoint = ptStart + direction;
			JigEdgeInfo item = new JigEdgeInfo(ptStart, endPoint, 0.0);
			this.m_DragCurveInfos.Add(item);
		}

		private void DrawModelLine(XYZ ptStart, XYZ direction)
		{
			XYZ ptEnd = ptStart + direction;
			Line line = null;
			if (!this.CreatYJKLine(ptStart, ptEnd, true, ref line))
			{
				return;
			}
			if (this.m_DirZ.IsAlmostEqualTo(XYZ.BasisZ) && this.m_DirX.IsAlmostEqualTo(XYZ.BasisX) && this.m_DirY.IsAlmostEqualTo(XYZ.BasisY))
			{
				DetailCurve detailCurve = this.m_Revit.Application.ActiveUIDocument.Document.Create.NewDetailCurve(this.m_DrawingView, line);
				if (detailCurve != null)
				{
					this.m_SubElementsList.Add(detailCurve.Id);
					return;
				}
			}
			else
			{
				Document document = this.m_Revit.Application.ActiveUIDocument.Document;
				Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(this.m_DirZ, ptStart);
				SketchPlane sketchPlane = SketchPlane.Create(document, plane);
				ModelLine modelLine = document.Create.NewModelCurve(line, sketchPlane) as ModelLine;
				if (modelLine != null)
				{
					this.m_SubElementsList.Add(modelLine.Id);
				}
			}
		}

		private bool CreatYJKLine(XYZ ptStart, XYZ ptEnd, bool bound, ref Line line)
		{
			if (Geometry.LessThan(ptStart.DistanceTo(ptEnd), Common.MMToFeet(0.8)))
			{
				return false;
			}
			line = this.m_Revit.Application.ActiveUIDocument.Document.CreatYJKLine(ptStart, ptEnd, bound);
			return true;
		}

		private int GetLength(string str)
		{
			if (str.Length == 0)
			{
				return 0;
			}
			Encoding encoding = new ASCIIEncoding();
			int num = 0;
			byte[] bytes = encoding.GetBytes(str);
			for (int i = 0; i < bytes.Length; i++)
			{
				if (bytes[i] == 63)
				{
					num += 2;
				}
				else
				{
					num++;
				}
			}
			return num;
		}

		private void DrawText(XYZ ptLeftTop, double width, double height, string text)
		{
			if (text.CompareTo("") == 0)
			{
				return;
			}
			XYZ zero = XYZ.Zero;
			string versionName = this.m_Revit.Application.Application.VersionName;
			RevitVersionFuncs.SetVerticalOffsetDirectionOfText(ref zero);
			if (this.m_CellAutoSize == TableDrawingToRevitKit._TagTextComosingMode.TCM_NONE)
			{
				StringComposingKit stringComposingKit = new StringComposingKit(text, width, height);
				double num = 0.0;
				double num2 = 0.0;
				List<string> list = new List<string>();
				stringComposingKit.ComosingHorizontalAndVertical(ref num, ref num2, ref list);
				num *= 0.85;
				TextNoteType textNoteType = this.GetTextNoteType(num / this.m_DrawingScale, num2);
				double num3 = height / (double)list.Count;
				for (int i = 0; i < list.Count; i++)
				{
					string text2 = list[i];
					XYZ xyz = ptLeftTop + this.m_DirX * width / 2.0;
					xyz -= this.m_DirY * ((double)i + 0.5) * num3;
					XYZ dirX = this.m_DirX;
					XYZ dirY = this.m_DirY;
					double lineWidth = num * num2 * (double)(text2.Length * 3);
					TextNote textNode = this.m_Revit.Application.ActiveUIDocument.Document.GetTextNode(this.m_DrawingView, xyz + zero * height / 2.0 * 0.5, dirX, dirY, lineWidth, (Autodesk.Revit.DB.TextAlignFlags)1152, text2);
					if (textNode != null)
					{
						if (textNoteType != null)
						{
							textNode.TextNoteType = textNoteType;
						}
						this.m_SubElementsList.Add(textNode.Id);
					}
				}
				return;
			}
			if (this.m_CellAutoSize == TableDrawingToRevitKit._TagTextComosingMode.TCM_VERTICAL_ZOOM)
			{
				StringComposingKit stringComposingKit2 = new StringComposingKit(text, width, height);
				List<string> list2 = new List<string>();
				stringComposingKit2.ComosingVertical(this.m_TextSize * this.m_DrawingScale, this.m_TextWidthScale, this.m_TableInfos.HMargin, ref list2);
				double num4 = height / (double)list2.Count;
				for (int j = 0; j < list2.Count; j++)
				{
					string text3 = list2[j];
					XYZ xyz2 = ptLeftTop + this.m_DirX * width / 2.0;
					xyz2 -= this.m_DirY * ((double)j + 0.5) * num4;
					XYZ dirX2 = this.m_DirX;
					XYZ dirY2 = this.m_DirY;
					double lineWidth2 = this.m_TextSize * this.m_TextWidthScale * (double)(text3.Length * 3);
					TextNote textNode2 = this.m_Revit.Application.ActiveUIDocument.Document.GetTextNode(this.m_DrawingView, xyz2 + zero * height / 2.0 * 0.5, dirX2, dirY2, lineWidth2, (Autodesk.Revit.DB.TextAlignFlags)1152, text3);
					if (textNode2 != null)
					{
						if (this.m_TextNoteType != null)
						{
							textNode2.TextNoteType = this.m_TextNoteType;
						}
						this.m_SubElementsList.Add(textNode2.Id);
					}
				}
				return;
			}
			XYZ xyz3 = ptLeftTop + this.m_DirX * width / 2.0;
			xyz3 -= this.m_DirY * height / 2.0;
			XYZ dirX3 = this.m_DirX;
			XYZ dirY3 = this.m_DirY;
			double lineWidth3 = this.m_TextSize * this.m_TextWidthScale * (double)(text.Length * 3);
			TextNote textNode3 = this.m_Revit.Application.ActiveUIDocument.Document.GetTextNode(this.m_DrawingView, xyz3 + zero * height / 2.0 * 0.5, dirX3, dirY3, lineWidth3, (Autodesk.Revit.DB.TextAlignFlags)1152, text);
			if (textNode3 != null)
			{
				if (this.m_TextNoteType != null)
				{
					textNode3.TextNoteType = this.m_TextNoteType;
				}
				this.m_SubElementsList.Add(textNode3.Id);
			}
		}

		private void DrawLegendComponent(XYZ ptLeftTop, double width, double height, string legendFileFillName, bool zoomImage)
		{
			if (!File.Exists(legendFileFillName))
			{
				return;
			}
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			try
			{
				Image image = Image.FromFile(legendFileFillName);
				int width2 = image.Width;
				int height2 = image.Height;
				double num = (double)width2 / ((double)height2 * 1.0);
				double val = width / height;
				double num2 = width;
				double num3 = height;
				if (Geometry.LessThan(num, val))
				{
					num2 = num3 * num;
				}
				else
				{
					num3 = num2 / num;
				}
				ImageImportOptions imageImportOptions = new ImageImportOptions();
				imageImportOptions.Placement = 0;
				XYZ xyz = ptLeftTop + this.m_DirX * width / 2.0;
				xyz -= this.m_DirY * height / 2.0;
				imageImportOptions.RefPoint = xyz;
				Element element = null;
				if (document.Import(legendFileFillName, imageImportOptions, this.m_DrawingView, out element) && element != null)
				{
					element.GetParameter(BuiltInParameter.RASTER_MAINTAIN_ASPECT_RATIO).Set(0);
					if (zoomImage)
					{
						element.GetParameter(BuiltInParameter.RASTER_SHEETWIDTH).Set(num2);
						element.GetParameter(BuiltInParameter.RASTER_SHEETHEIGHT).Set(num3);
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private TextNoteType GetTextNoteType(double fontHeight, double fontRate)
		{
			string text = "";
			string name = this.m_TextNoteType.Name;
			int num = name.IndexOf(" ");
			if (num < 0)
			{
				text = "宋体";
			}
			else
			{
				text = name.Substring(0, num);
			}
			TextNoteType textNoteType = null;
			double num2 = 1000.0;
			foreach (Element element in new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document).OfClass(typeof(TextNoteType)).ToElements())
			{
				TextNoteType textNoteType2 = element as TextNoteType;
				if (textNoteType2 != null && textNoteType2.Name.IndexOf(text) > -1)
				{
					Parameter parameter = textNoteType2.get_Parameter(BuiltInParameter.TEXT_SIZE);
					if (parameter != null)
					{
						double num3 = parameter.AsDouble();
						parameter = textNoteType2.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE);
						if (parameter != null)
						{
							double d = parameter.AsDouble();
							if (GeoUtil.IsEqual(fontHeight, num3, 0.001) && GeoUtil.IsEqual(fontRate, d, 0.001))
							{
								return textNoteType2;
							}
							double num4 = Math.Abs(fontHeight - num3);
							if (num4 < num2)
							{
								num2 = num4;
								textNoteType = textNoteType2;
							}
						}
					}
				}
			}
			if (textNoteType == null)
			{
				return null;
			}
			string text2 = text + " " + AssistFunc.feetToMM(fontHeight).ToString("F2") + "mm";
			TextNoteType textNoteType3 = textNoteType.Duplicate(text2) as TextNoteType;
			textNoteType3.get_Parameter(BuiltInParameter.TEXT_SIZE).Set(fontHeight);
			textNoteType3.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).Set(fontRate);
			return textNoteType3;
		}

		private Group CreateGroup()
		{
			ElementSet elementSet = new ElementSet();
			foreach (ElementId id in this.m_SubElementsList)
			{
				Element element = RevitVersionFuncs.GetElement(this.m_Revit.Application.ActiveUIDocument.Document, id);
				elementSet.Insert(element);
			}
			Group result;
			try
			{
				Group group = null;
				if (elementSet.Size > 0)
				{
					group = RevitVersionFuncs.NewGroup(this.m_Revit.Application.ActiveUIDocument.Document, elementSet);
				}
				group.GroupType.Name = "TableGroup";
				result = group;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = null;
			}
			return result;
		}

		private void ZoomElement(Element ele)
		{
			UIDocument activeUIDocument = this.m_Revit.Application.ActiveUIDocument;
			UIView uiview = null;
			foreach (UIView uiview2 in activeUIDocument.GetOpenUIViews())
			{
				if (uiview2.ViewId.Equals(activeUIDocument.ActiveView.Id))
				{
					uiview = uiview2;
					break;
				}
			}
			if (uiview == null)
			{
				return;
			}
			BoundingBoxXYZ boundingBoxXYZ = ele.get_BoundingBox(activeUIDocument.ActiveView);
			if (boundingBoxXYZ == null)
			{
				return;
			}
			double num = 0.1;
			XYZ xyz = (boundingBoxXYZ.Min - boundingBoxXYZ.Max).Normalize();
			XYZ xyz2 = -xyz;
			double length = (boundingBoxXYZ.Min - boundingBoxXYZ.Max).GetLength();
			XYZ xyz3 = boundingBoxXYZ.Min + xyz * length * num;
			XYZ xyz4 = boundingBoxXYZ.Max + xyz2 * length * num;
			uiview.ZoomAndCenterRectangle(xyz3, xyz4);
		}

		private double CellLeft(int cellColumnRangStart)
		{
			double num = 0.0;
			for (int i = 0; i < cellColumnRangStart; i++)
			{
				num += this.m_ColumnWidthList[i] * this.m_DrawingScale;
			}
			return num;
		}

		private double CellTop(int cellRowRangStart)
		{
			double num = 0.0;
			for (int i = 0; i < cellRowRangStart; i++)
			{
				num += this.m_RowHeightList[i] * this.m_DrawingScale;
			}
			return num;
		}

		private double CellWidth(List<int> cellColumnRang)
		{
			double num = 0.0;
			foreach (int index in cellColumnRang)
			{
				num += this.m_ColumnWidthList[index] * this.m_DrawingScale;
			}
			return num;
		}

		private double CellHeight(List<int> cellRowRang)
		{
			double num = 0.0;
			foreach (int index in cellRowRang)
			{
				num += this.m_RowHeightList[index] * this.m_DrawingScale;
			}
			return num;
		}

		private void CalculateTableWidth()
		{
			this.m_TableWidth = 0.0;
			foreach (double num in this.m_ColumnWidthList)
			{
				this.m_TableWidth += num * this.m_DrawingScale;
			}
		}

		private void CalculateHeaderHeight()
		{
			this.m_HeaderHeight = 0.0;
			int i = 0;
			int rowSize = this.m_TableInfos.Head.RowSize;
			while (i < rowSize)
			{
				this.m_HeaderHeight += this.m_RowHeightList[i] * this.m_DrawingScale;
				i++;
			}
		}

		private void CalculateBodyHeight()
		{
			this.m_BodyHeight = 0.0;
			for (int i = this.m_TableInfos.Head.RowSize; i < this.m_RowHeightList.Count; i++)
			{
				this.m_BodyHeight += this.m_RowHeightList[i] * this.m_DrawingScale;
			}
		}

		private ExternalCommandData m_Revit;

		private Autodesk.Revit.DB.View m_DrawingView;

		private TableInfos m_TableInfos;

		private TextNoteType m_TextNoteType;

		private double m_TextSize = AssistFunc.mmToFeet(3.5);

		private double m_TextWidthScale = 0.7;

		private double m_DrawingScale = 1.0;

		private TableDrawingToRevitKit._TagTextComosingMode m_CellAutoSize;

		private List<double> m_ColumnWidthList = new List<double>();

		private List<double> m_RowHeightList = new List<double>();

		private List<JigEdgeInfo> m_DragCurveInfos = new List<JigEdgeInfo>();

		private XYZ m_ptLeftTop;

		private List<ElementId> m_SubElementsList = new List<ElementId>();

		private double m_TableWidth;

		private double m_HeaderHeight;

		private double m_BodyHeight;

		private const double m_CRowMinHeight = 0.026246719160104987;

		private XYZ m_DirX = XYZ.BasisX;

		private XYZ m_DirY = XYZ.BasisY;

		private XYZ m_DirZ = XYZ.BasisZ;

		private enum _TagTextComosingMode
		{
			TCM_NONE,
			TCM_HORIZONTAL_ZOOM,
			TCM_VERTICAL_ZOOM
		}
	}
}
